package com.seven.micro_service.Config;

import com.seven.micro_service.Interceptor.LoginInterceptor;
import com.seven.micro_service.tools.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 *自定义拦截类的配置类
 */
@Configuration
public class InterceptConfig implements WebMvcConfigurer {
    private LoginInterceptor loginInterceptor;

    @Autowired
    public void setLoginInterceptor(LoginInterceptor loginInterceptor) {
        this.loginInterceptor = loginInterceptor;
    }

    @Bean
    public JwtUtil jwtUtil() {
        return new JwtUtil();
    }

    //注册一个我们登录的拦截类
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //我们需要排除登录,和注册的拦截(特别注意,我们还需要取消拦截静态资源的访问)
        String[] exclude = new String[]{"/login",};
        //1.需要对我们自定义的拦截器进行注册
        registry.addInterceptor(loginInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns(exclude);//拦截路径
        WebMvcConfigurer.super.addInterceptors(registry);
    }
}